﻿using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using System.Linq;
using ChuanYe.CountyFireManagement.Model;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class FireFirstReportBLL
    {
        #region 构造单例
        private FireFirstReportBLL() { }
        private static FireFirstReportBLL _instance;
        public static FireFirstReportBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new FireFirstReportBLL());
            }
        }
        #endregion

        public ResultDto<List<FireMQTTResponseModel>> Add(Firefirstreport info)
        {
            ResultDto<List<FireMQTTResponseModel>> result = new ResultDto<List<FireMQTTResponseModel>>();
            var fireInfo = BLL.FireInfoBLL.Instance.Get(info.FireId);
            if (fireInfo == null)
            {
                result.Status = -1;
                result.Message = "FireInfo Is Not Exist";
                return result;
            }
            var lastRecordInfo = BLL.RecordBLL.Instance.GetLast(info.FireId, null);
            //if (lastRecordInfo == null
            //    || (lastRecordInfo.FirstOrDefault().Type != (int)RecordType.火灾录入
            //       && lastRecordInfo.FirstOrDefault().Type != (int)RecordType.首报未按时操作))
            //{
            //    result.Status = -2;
            //    result.Message = "FireInfo Status Is Not Added";
            //    return result;
            //}
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                using (TransactionScope trans = new TransactionScope())
                {
                    //首报写入
                    ef.Firefirstreport.Add(info);
                    ef.Entry<Firefirstreport>(info).State = EntityState.Added;
                    ef.SaveChanges();

                    var userInfo = ef.Userinfo.Where(p => p.UserId == info.UserId).FirstOrDefault();
                    //处置记录
                    Record record = new Record();
                    record.FireId = fireInfo.FireId;
                    record.HandleTime = info.TransDatetime;
                    record.Type = (int)RecordType.火灾首报;
                    record.UserId = info.UserId;
                    record.Content = info.FirstReportContent;
                    record.AddTime = info.TransDatetime;
                    record.Ip = info.Ip;
                    record.Applicant = userInfo.RealName;
                    record.CustomData = info.FirstReportId.ToString();
                    ef.Record.Add(record);
                    ef.Entry<Record>(record).State = EntityState.Added;

                    //消息
                    Message message = new Message();
                    message.FireId = fireInfo.FireId;
                    message.Type = (int)MessageType.火灾首报;
                    string happenDatetime = info.TransDatetime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                    message.Content = string.Format(FireInfoTool.GetFireInfoMessageFormat(message.Type), fireInfo.FireName, happenDatetime);
                    message.SendType = (int)MessageSendType.下级向上级发送;
                    message.IsRead = 0;
                    message.HandleTime = info.TransDatetime;
                    message.SendUserId = info.UserId.Value;
                    message.Ip = fireInfo.Ip;
                    ef.Message.Add(message);
                    ef.Entry<Message>(message).State = EntityState.Added;
                    ef.SaveChanges();

                    string firstTime = fireInfo.TransDatetime.Value.AddMinutes(30).ToString("yyyy年MM月dd日HH时mm分");
                    Message sbMessage = new Message();
                    sbMessage.FireId = info.FireId;
                    sbMessage.Type = (int)MessageType.续报倒计时提示;
                    sbMessage.Content = string.Format(FireInfoTool.GetFireInfoMessageFormat(sbMessage.Type), fireInfo.FireName, firstTime);
                    sbMessage.SendType = (int)MessageSendType.上级向下级发送;
                    sbMessage.IsRead = 0;
                    sbMessage.HandleTime = info.TransDatetime;
                    sbMessage.SendUserId = info.UserId.Value;
                    sbMessage.Ip = info.Ip;
                    ef.Message.Add(sbMessage);
                    ef.Entry<Message>(sbMessage).State = EntityState.Added;


                    //移除首报过期等待
                    var existFireexpire = ef.Fireexpire.Where(p => p.FireId == info.FireId && p.RecordType == (int)RecordType.首报未按时操作).FirstOrDefault();
                    if (existFireexpire != null)
                    {
                        ef.Fireexpire.Add(existFireexpire);
                        ef.Entry<Fireexpire>(existFireexpire).State = EntityState.Deleted;
                    }

                    //增加续报过期等待
                    var nextValue = 30;
                    var continueTime=info.TransDatetime.Value.AddMinutes(nextValue).ToString("yyyy年MM月dd日HH时mm分");
                    Fireexpire fireexpire = new Fireexpire();
                    fireexpire.StartTime = info.TransDatetime.Value;
                    fireexpire.Value = nextValue;
                    fireexpire.FireId = fireInfo.FireId;
                    fireexpire.RecordType = (int)RecordType.续报未按时操作;
                    fireexpire.MessageType = (int)MessageType.续报未作提示;
                    string msContent = string.Format(FireInfoTool.GetFireInfoMessageFormat(fireexpire.MessageType), fireInfo.FireName, continueTime);
                    fireexpire.MessageContent = msContent;
                    ef.Fireexpire.Add(fireexpire);
                    ef.Entry<Fireexpire>(fireexpire).State = EntityState.Added;
                    ef.SaveChanges();
                    trans.Complete();
                    #region 装载消息
                    List<Message> messages = new List<Message>() { message, sbMessage };
                    result.FirstParam = MessageBLL.Instance.TransMessageToFireMQTTResponseModel(messages,record.Type.Value);
                    #endregion
                }
                //更改已读
                BLL.MessageBLL.Instance.ChangeMessageReadStatus(info.FireId,
                                                                new List<int>() { (int)MessageType.首报倒计时提示 }, 1);
                return result;
            }
        }
    }
}
